#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDateTime>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_createArray_clicked()
{
    int datacount = ui->dataCount->text().toInt();
    if (datacount<=0) {
        QMessageBox::warning(this, "Error", "data count can't less than 00");
        return;
    }
    QJsonArray array;
    for(int i=0; i<datacount; i++) {
        array.push_back(i);
    }
    QJsonDocument doc(array);
    jsonString = doc.toJson();
    ui->logtext->appendPlainText(jsonString);
}

void MainWindow::on_createObject_clicked()
{
    int datacount = ui->dataCount->text().toInt();
    if (datacount<=0) {
        QMessageBox::warning(this, "Error", "data count can't less than 00");
        return;
    }
    QJsonObject obj;
    for(int i=0; i<datacount; i++) {
        obj.insert(QString("obj%1").arg(i), QJsonObject());
    }
    QJsonDocument doc(obj);
    jsonString = doc.toJson();
    ui->logtext->appendPlainText(jsonString);
}

void MainWindow::on_runTest_clicked()
{
    int runcount = ui->runCount->text().toInt();
    if (runcount<=0) {
        QMessageBox::warning(this, "Error", "runloop count can't less than 00");
        return;
    }

    qint64 time_start = QDateTime::currentMSecsSinceEpoch();
    ui->logtext->appendPlainText("start(msec):"+QString::number(time_start));

    for(int i=0; i<runcount; i++) {
        QJsonDocument doc = QJsonDocument::fromJson(jsonString.toLocal8Bit());
    }

    qint64 time_end = QDateTime::currentMSecsSinceEpoch();
    ui->logtext->appendPlainText("stop(msec):"+QString::number(time_end));
    ui->logtext->appendPlainText("time used(msec):"+QString::number(time_end-time_start));
    ui->logtext->appendPlainText("process(items/sec):"+QString::number(runcount*1000/(time_end-time_start)));
}

void MainWindow::on_runTest2_clicked()
{
    int datacount = ui->dataCount->text().toInt();
    if (datacount<=0) {
        QMessageBox::warning(this, "Error", "data count can't less than 00");
        return;
    }

    int runcount = ui->runCount->text().toInt();
    if (runcount<=0) {
        QMessageBox::warning(this, "Error", "runloop count can't less than 00");
        return;
    }

    qint64 time_start = QDateTime::currentMSecsSinceEpoch();
    ui->logtext->appendPlainText("start(msec):"+QString::number(time_start));

    qint64 count = 0;
    for(int i=0; i<runcount; i++) {
        for (int j=0; j<datacount; j++) {
            count += i*j;
        }
    }

    qint64 time_end = QDateTime::currentMSecsSinceEpoch();
    ui->logtext->appendPlainText("stop(msec):"+QString::number(time_end));
    ui->logtext->appendPlainText("time used(msec):"+QString::number(time_end-time_start));
    ui->logtext->appendPlainText("process(items/sec):"+QString::number(runcount*1000/(time_end-time_start)));
}
